package cn.truistic.enmicromsg.wx.wx_friendscircle;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.truistic.enmicromsg.R;
import cn.truistic.enmicromsg.base.BaseActivity;
import cn.truistic.enmicromsg.common.dialog.ContentDialog;
import cn.truistic.enmicromsg.common.interfaces.DialogContentCallback;
import cn.truistic.enmicromsg.common.util.PreferenceHelper;
import cn.truistic.enmicromsg.common.util.WxHeple;

/**
 * 微信朋友圈
 * name: ts
 * time: 10点54分   2018 / 6 /15
 */
public class FriendsCircleActivity extends BaseActivity implements DialogContentCallback {

    private Toolbar mToolbar;
    private EditText mEditText;
    private Button mBut_begin;
    private Button mBut_edit;
    private TextView tv_comment;
    private ContentDialog contentDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends_circle);
        initView();
        initListener();
    }

    //设置监听
    private void initListener() {
        //开起辅助
        mBut_begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = mEditText.getText().toString().trim();     //输入人数
                int mNearTextNum = 0;
                try {
                    mNearTextNum = Integer.parseInt(number);    //String转化成Int
                } catch (NumberFormatException e) {
                    Toast.makeText( mContext, "输入有误，重新输入..", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mNearTextNum <= 0) {
                    Toast.makeText( mContext, "评论条数不能为零", Toast.LENGTH_SHORT).show();
                } else if (mNearTextNum > 60) {
                    Toast.makeText( mContext, "评论条数不能大于60条", Toast.LENGTH_SHORT).show();
                } else {
                    boolean isOpen = isAccessibilitySettingsOn();
                    if (isOpen) {
                        PreferenceHelper.commitInt("mFriendTextNum", mNearTextNum);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                mEnApplication.setFriendCircle(true);  //开启朋友圈模块
                                mEnApplication.setAllowFriendCircle(true);
                                WxHeple.execShellCmd("am force-stop com.tencent.mm");
                                WxHeple.execShellCmd("input keyevent 3");
                                try {
                                    Thread.sleep(2500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                WxHeple.intentWechat(mContext); //跳转微信主界面
                            }
                        }).start();
                    }
                }
            }
        });

        //设置输入回复内容
        mBut_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (contentDialog != null) {
                    contentDialog.show();
                }
            }
        });
    }

    //初始化控件！
    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.mToolbar);
        mEditText = (EditText) findViewById(R.id.et_content);
        mBut_begin = (Button) findViewById(R.id.but_begin);
        mBut_edit = (Button) findViewById(R.id.but_edit);
        tv_comment = (TextView) findViewById(R.id.tv_comment);
        setSupportActionBar(mToolbar);
        contentDialog = new ContentDialog(this, this);
       String content =  PreferenceHelper.getString("FriendsContent","");
        if (!TextUtils.isEmpty(content)){
            tv_comment.setText("评论内容: " + content);
        }
    }

    @Override
    public void Negative() {
        if (contentDialog != null)
            contentDialog.dismiss();
    }

    @Override
    public void Positive(String content) {
        tv_comment.setText("评论内容: " + content);
        PreferenceHelper.commitString("FriendsContent", content); //把评论内容保存到本地
    }
}
